/**
 * created by zhouli on 2019/1/9
 */
import config from 'config';
import httpFetch from 'share/httpFetch';
import errorMessage from 'share/errorMessage';
import {deepCopy} from "share/common";
import configureStore from 'stores';
import {setPermissionData} from 'actions/permission-data';

export default {
  //检查是否有条件正在编辑
  checkConditionIsEditing(permissionData) {
    if (permissionData.conditionViewDTOS &&
      permissionData.conditionViewDTOS.length &&
      permissionData.conditionViewDTOS.length > 0) {
      let len = permissionData.conditionViewDTOS.length;
      for (let i = 0; i < len; i++) {
        let item = permissionData.conditionViewDTOS[i];
        if (item.isEditing) {
          return true;
        }
      }
      return false;
    }
    else {
      return false;
    }
  },
  //替换数据权限中的条件
  getPermissionData(condition, originPermissionData, isDispatch) {
    let permissionData = deepCopy(originPermissionData);
    permissionData.conditionViewDTOS = permissionData.conditionViewDTOS.map(item => {
      if (item.sequence === condition.sequence) {
        item = condition;
      }
      return item;
    });
    if (isDispatch) {
      configureStore.store.dispatch(setPermissionData(permissionData));
    }
    return permissionData;
  },
  //id获取组织权限详情
  getDataPermissionTableItemDetail(params) {
    // let params = {
    //   "dataPermissionId": "周",
    // }
    return new Promise((resolve, reject) => {
      httpFetch.get(config.baseUrl + '/api/security/dataPermission/id', params)
        .then((response) => {
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  //POST：新建组织权限详情(不支持批量，只能一个一个添加)
  createDataPermissionTableItemDetail(params) {
    // dataPermissionId 是传在url后面的
    // let params = {
    //   "conditionDetails": [
    //     {
    //       "conditionProperty": "Company",
    //       "enabled": true,
    //       "conditionLogic": "E",
    //       "conditionValues": [
    //         {
    //           "conditionValue": "889120",
    //           "description": "公司1"
    //         },
    //         {
    //           "conditionValue": "889121",
    //           "description": "公司2"
    //         }
    //       ]
    //     },
    //     {
    //       "conditionProperty": "Department",
    //       "enabled": true,
    //       "conditionLogic": "I",
    //       "conditionValues": [
    //         {
    //           "conditionValue": "974461721816872632",
    //           "description": "bu'men部门1"
    //         },
    //         {
    //           "conditionValue": "974461721816872634",
    //           "description": "部门2"
    //         }
    //       ]
    //     }
    //   ]
    // }
    return new Promise((resolve, reject) => {
      httpFetch.post(config.baseUrl + '/api/security/dataPermission/detail?dataPermissionId=' + params.dataPermissionId, params)
        .then((response) => {
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  //更新组织权限详情
  updateDataPermissionTableItemDetail(params) {
    // dataPermissionId 是传在url后面的
    // let params = {
    //   "permissionProp": "other",
    //   "dataPermissionId": "1077177895258062850",
    //   "tenantId": 985801821654523905,
    //   "conditionDetails": [
    //     {
    //       "conditionProperty": "Company",
    //       "enabled": true,
    //       "conditionLogic": "E",
    //       "conditionValues": [
    //         {
    //           "conditionValue": "889120",
    //           "description": "公司1"
    //         },
    //         {
    //           "conditionValue": "889121",
    //           "description": "公司2"
    //         }
    //       ]
    //     },
    //     {
    //       "conditionProperty": "Department",
    //       "enabled": true,
    //       "conditionLogic": "I",
    //       "conditionValues": [
    //         {
    //           "conditionValue": "974461721816872632",
    //           "description": "bu'men部门1"
    //         },
    //         {
    //           "conditionValue": "974461721816872634",
    //           "description": "部门2"
    //         }
    //       ]
    //     }
    //   ]
    // }
    return new Promise((resolve, reject) => {
      httpFetch.put(config.baseUrl + '/api/security/dataPermission/detail?dataPermissionId=' + params.dataPermissionId, params)
        .then((response) => {
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  },
  //删除单个数据条件
  deleteDataPermissionTableItemDetail(params) {
    //这两个参数都是放url后面的
    // let params = {
    //   dataPermissionId,
    //   seq
    // }
    return new Promise((resolve, reject) => {
      httpFetch.delete(config.baseUrl + '/api/security/dataPermission/detail', params)
        .then((response) => {
          resolve(response)
        })
        .catch(function (err) {
          errorMessage(err.response);
          reject(err.response);
        })
    })
  }
}
